<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getOrderList()">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList()"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="100"></el-table-column>
        <el-table-column label="是否付款" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0' " type="warning">未付款</el-tag>
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="100"></el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="180">
          <template slot-scope="scope">{{parseTime(scope.row.create_time,'{y}-{m}-{d} {h}:{i}:{s}')}}</template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <el-button type="primary" size="mini" @click="showEditDialog()">编辑</el-button>
          <el-button type="info" size="mini" @click="getOrderInfoprogress()">详细</el-button>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="省市区/县">
          <el-cascader v-model="citySelectkeys" :options="cityData" :props="cityProp"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流信息对话框 -->
    <el-dialog title="物流信息" :visible.sync="showDialogVisible" width="50%">
      <el-timeline>
        <el-timeline-item v-for="(item, index) in orderInfoList" :key="index" :timestamp="item.time" :color="'green'">{{item.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>

  </div>
</template>

<script lang="ts">
// 导入省市区数据
import { parseTime } from "@/untils/index";
import cityData from "@/assets/city.js";
import { order, orderNo } from "@/request/api";
import Vue from "vue";
import Component from "vue-class-component";
@Component
export default class Order extends Vue {
  queryInfo: {
    query: string;
    pagenum: number;
    pagesize: number;
  } = {
    query: "",
    pagenum: 1,
    pagesize: 5,
  };
  orderList: Array<any> = [];
  total: number = 0;
  // 编辑框显示与否
  editDialogVisible: boolean = false;
  // 省市区数据
  cityData: Array<any> = cityData;
  citySelectkeys: Array<any> = [];
  cityProp: {
    label: string;
    value: string;
    children: string;
  } = {
    label: "label",
    value: "value",
    children: "children",
  };
  form: {} = {};
  // 物流信息查看对话框
  showDialogVisible: boolean = false;
  // 记录物流信息数据
  orderInfoList: Array<any> = [];
  created() {
    this.getOrderList();
  }
  // 获取订单列表数据
  async getOrderList() {
    const { data, meta } = await order(this.queryInfo);
    if (meta.status !== 200) return this.$message.error("获取订单列表失败");
    this.$message.success("获取订单列表成功");
    this.orderList = data.goods;
    this.total = data.total;
  }
  // 点击页数的事件
  handleCurrentChange(newCurrent: number) {
    this.queryInfo.pagenum = newCurrent;
    this.getOrderList();
  }
  // 每页数量改变触发的事件
  handleSizeChange(newPageSize: number) {
    this.queryInfo.pagesize = newPageSize;
    this.getOrderList();
  }
  // 打开编辑对话框
  showEditDialog() {
    this.editDialogVisible = true;
  }
  // 根据单号获取对应的物流信息
  async getOrderInfoprogress() {
    // 测试物流单号
    const { data, meta } = await orderNo();
    if (meta.status !== 200) return this.$message.error("获取物流信息失败");
    this.$message.success("获取物流信息成功");
    this.orderInfoList = data;
    this.showDialogVisible = true;
  }
  parseTime(time: Object | string | number, format: string) {
    return parseTime(time, format);
  }
}
</script>

<style lang="less" scoped>
.el-pagination {
  margin: 15px 0 0 0;
}
.el-cascader {
  width: 100%;
}
</style>